
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local Timer = require("core.Timer")
local Skill = require("skill.Skill")
local CombatCalc = require("skill.CombatCalc")
local EffectLogic = require("scene.EffectLogic")
list = list or {}
if not pool then
    pool = {}
    for i = 1, 8192 do
        pool[i] = {}
    end
end


function create(owner,bodyID,bottomID,skill_id,skill_level,maxCount,aoeSpeed,dir,comboType,comboArg1,comboArg2)
    if not bodyID then
        assert()
    end
    local aoe = pool[#pool]
    if aoe then
        pool[#pool] = nil
    else
        return
    end
    Obj.create(aoe, Obj.TYPE_AOE)
    local root = owner.owner or owner
    aoe.owner = aoe.owner or {}
    aoe.owner.id = root.id
    aoe.owner.obj_type = root.obj_type
    aoe.owner.uid = root.uid
    CombatCalc.setAttrMount(aoe,owner)		--保存主人属性
    aoe.camps = aoe.owner.camps and aoe.owner.camps or 0
    aoe.run = false
    aoe.bodyID = bodyID
    aoe.bottomID = bottomID
    aoe.skill_id = skill_id
    aoe.skill_level = skill_level
    aoe.maxCount = maxCount
    aoe.aoeSpeed = aoeSpeed * 1000

    aoe.dir = dir
    aoe.comboType = comboType
    aoe.comboArg1 = comboArg1
    aoe.comboArg2 = comboArg2

    aoe.count = 0
    aoe.lastTime = 0
    aoe.lastMoveTime = 0
    aoe.speed = nil
    aoe.pathLenLocus = nil

    if list[aoe.id] then
        assert()
    end
    list[aoe.id] = aoe
    return aoe
end

function getObjAdd(aoe)
    local mm = Msg.gc.GC_ADD_AOE
    mm.owner_uuid = "" --(not aoe.attackType or (aoe.attackType~=1 and aoe.attackType~=2) or not owner or not owner.scene_id) and 0 or aoe.owner.id
    mm.owner_combatSide = 0
    mm.owner_combatMode = 0
    mm.obj_id = aoe.id
    mm.bodyID = aoe.bodyID
    mm.bottomID = aoe.bottomID
    mm.speed = aoe.speed or 0
    mm.bodyEffectConfig =  EffectLogic.getConfig(aoe.bodyID)
    mm.bottomEffectConfig =  EffectLogic.getConfig(aoe.bottomID)
    mm.x = aoe.x
    mm.y = aoe.y
    mm.dir = aoe.dir or 0
    mm.show = 1
    mm.comboType = aoe.comboType or 0
    mm.comboArg1 = aoe.comboArg1 or 0
    mm.comboArg2 = aoe.comboArg2 or 0
    mm.isDie =0
    mm.onlyShowOwner = 0

    mm.points[0] = aoe.path_len * 2
    for i = 1, aoe.path_len do
        local j = aoe.path_len + 1 - i
        mm.points[2 * i - 1] = aoe.path[2 * j - 1]
        mm.points[2 * i] = aoe.path[2 * j]
    end

    return mm
end

local empty = {}
function getBuf()
    return empty,0
end
function setBufLen()

end


function getName()
    return "aoe_name"
end

function getHp()
    return 1
end

function destroy(aoe)
    if not list[aoe.id] then
        assert()
    end
    pool[#pool + 1] = aoe
    aoe.die = 1
    list[aoe.id] = nil
    Obj.destroy(aoe)
    aoe.die = nil
end

function onLoop(aoe)
    local owner = Obj.getObj(aoe.owner.id, aoe.owner.uid)
    local now = Timer.now
    local dieTime = aoe.dieTime
    if not owner or (owner.obj_type==Obj.TYPE_HUMAN and (owner.db.hp<1 or owner.scene_id ~= aoe.scene_id)) or (dieTime and now>=dieTime) then
        destroy(aoe)
    else
        if aoe.count<aoe.maxCount and now - aoe.lastTime >= aoe.aoeSpeed then
            aoe.lastTime = now
            aoe.count = aoe.count+1
            if 0 < aoe.skill_id then
                Skill.add(aoe,aoe.skill_id,aoe.x,aoe.y,aoe.x,aoe.y,nil,aoe.skill_level)
            end
        end

        local targetID = aoe.targetID
        if targetID then	--锁定
            local target = Obj.objs[targetID]
            if target and now - aoe.lastMoveTime >500 then
                aoe.lastMoveTime = now
                Obj.move(aoe, target.x, target.y)
            end
        elseif aoe.pathLocus and aoe.path_len<1 then	--轨迹移动
            aoe.path = aoe.path or {}
            aoe.path_len = aoe.pathLenLocus
            for i=1,aoe.pathLenLocus do
                aoe.path[i] = aoe.pathLocus[i]
            end
            Obj.movePath(aoe)
        end
    end
end

function onTimer()
    for _,aoe in pairs(list) do
        if aoe.run then
            destroy(aoe)
        else

            aoe.run = true
            onLoop(aoe)
            aoe.run = false
        end
    end
end

function onSkill()

end
